package com.my.juc.readwrite;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/3/6 15:45
 */
public class BlockingQueueDemo {

    public static void main(String[] args) throws InterruptedException {
        // 创建阻塞队列
        BlockingQueue<String> bq = new ArrayBlockingQueue<>(3);
        
        // 第一组
        /*System.out.println("bq.add(\"a\") = " + bq.add("a"));
        System.out.println("bq.add(\"b\") = " + bq.add("b"));
        System.out.println("bq.add(\"c\") = " + bq.add("c"));
        System.out.println("bq.element() = " + bq.element());

        // 队列已满 会抛出异常IllegalStateException: Queue full
        //System.out.println("bq.add(\"c\") = " + bq.add("d"));
        System.out.println(bq.remove());
        System.out.println(bq.remove());
        System.out.println(bq.remove());
        
        // NoSuchElementException
        //System.out.println(bq.remove());
        
        
        // 第二组
        System.out.println("bq.offer(\"a\") = " + bq.offer("a"));
        System.out.println("bq.offer(\"b\") = " + bq.offer("b"));
        System.out.println("bq.offer(\"c\") = " + bq.offer("c"));
        // false
        System.out.println("bq.offer(\"d\") = " + bq.offer("d"));

        System.out.println("bq.poll() = " + bq.poll());
        System.out.println("bq.poll() = " + bq.poll());
        System.out.println("bq.poll() = " + bq.poll());
        // null
        System.out.println("bq.poll() = " + bq.poll());*/
        
        /*bq.put("a");
        bq.put("b");
        bq.put("c");
        // 队列已满，会阻塞，当不满时不会阻塞
        //bq.put("d");

        System.out.println("bq.take() = " + bq.take());
        System.out.println("bq.take() = " + bq.take());
        System.out.println("bq.take() = " + bq.take());
        // 队列为空，没有元素，会阻塞，不为空时不会阻塞
        System.out.println("bq.take() = " + bq.take());*/

        bq.offer("a");
        bq.offer("b");
        bq.offer("c");
        bq.offer("c", 3L, TimeUnit.SECONDS);


    }
    
}
